package com.example.f8399.testdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.f8399.testdemo.BR;
import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.daoEntity.ExampleEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by f8399 on 2018/7/28.
 */

public class ExampleAdapter extends RecyclerView.Adapter<BindingViewHolder>{

    private Context mContext;
    private List<ExampleEntity> mlist = new ArrayList<>();
    private LayoutInflater mInflater;

    public ExampleAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public ExampleAdapter(Context context, List<ExampleEntity> mlist) {
        mContext = context;
        this.mlist = mlist;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding inflate = DataBindingUtil.inflate(mInflater, R.layout.item_example, parent, false);
        return new BindingViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
        ViewDataBinding binding = holder.getBinding();
        binding.setVariable(BR.item, mlist.get(position));

    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }



    public void setData(List<ExampleEntity> list) {
        mlist.clear();
        mlist.addAll(list);
        notifyDataSetChanged();
    }
}
